#include "Timer.h"

using namespace Kernel;

Timer::Timer()
:	m_Interval(0) , m_Clock(0) ,
	m_Enable(false)
{
#if defined(WIN32) || defined(_XBOX)
	QueryPerformanceFrequency(&m_Freq);
#endif
}

Timer::~Timer()
{

}

bool Timer::IsExpired()
{
	return GetTime() - m_Clock > m_Interval;
}

void Timer::Update()
{
	if (!m_Enable)
		return;

	static kint64 time;

#if defined(WIN32) || defined(_XBOX)

	static LARGE_INTEGER nTime;
	QueryPerformanceCounter(&nTime);
	time = (nTime.QuadPart) * 1000 / m_Freq.QuadPart;

#else

	static timeval tv;
	gettimeofday(&tv, 0);
	time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);

#endif

 	if (time - m_Clock > m_Interval)
 	{
		//use ontick

 		OnTick();
 
 		m_Clock += m_Interval;
 	}
}

kint64 Timer::GetTime()
{
#if defined(WIN32) || defined(_XBOX)

	static LARGE_INTEGER nTime;
	QueryPerformanceCounter(&nTime);
	return (nTime.QuadPart) * 1000 / m_Freq.QuadPart;

#else

	static timeval tv;
	gettimeofday(&tv, 0);
	return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);

#endif
}

void Timer::OnTick()
{
	//NULL
}
